package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.base.domain.LottoHistory;
import cn.wolfcode.trip.base.domain.SignIn;
import cn.wolfcode.trip.base.mapper.SignInMapper;
import cn.wolfcode.trip.base.service.ILottoHistoryService;
import cn.wolfcode.trip.base.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 签到控制器
 */
@RestController
@RequestMapping("lottoHistorys")
public class LottoHistoryController {
    @Autowired
    private ILottoHistoryService lottoHistoryService;
    @Autowired
    private SignInMapper signInMapper;

    /**
     * 根据用户id,和当天日期,获取签到对象的接口
     * 1.资源 /lottoHistorys/{userId}
     * 2.请求方式 post
     * 3.请求参数 抽奖对象
     * 4.返回结果 JsonResult
     */
    //新增用户抽奖记录
   @PostMapping
    public JsonResult save(LottoHistory lottoHistory) {
       SignIn signIn = signInMapper.selectByPrimaryKey(lottoHistory.getUserId());
       if(signIn.getIntegral()>99){
           signIn.setIntegral(signIn.getIntegral() - 100);
           signIn.setId(lottoHistory.getUserId());
           signInMapper.updateIntegral(signIn);
       }
       lottoHistoryService.save(lottoHistory);
        return new JsonResult();
    }

    //获取当前用户抽奖历史
    @GetMapping("{userId}")
    public List<LottoHistory> getSignInDays(@PathVariable Long userId){

        return lottoHistoryService.queryLottoHistory(userId);
    }
}
